#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>

#include "common/utils.hpp"

std::unordered_map<int, int> fre;

void traversal(TreeNode* node) {
    if (node == nullptr) return;

    traversal(node->left);
    fre[node->val]++;
    traversal(node->right);
}

/*
中序遍历得到的结果是一个递增有序序列
*/
std::vector<int> findMode(TreeNode* root) {
    traversal(root);

    int maxCount = 0;
    std::vector<int> ret;
    for (const auto& [key, value] : fre) {
        if (value > maxCount) {
            ret.clear();
            ret.push_back(key);
            maxCount = value;
        } else if (value < maxCount) {
            
        } else {
            ret.push_back(key);
        }
    }

    return ret;
}


extern TreeNode* commonRoot;

int main() {
    std::vector<int> nums = {1,0,1,0,0,1,1,0};
    TreeNode* node = createTreeNodeByArray(nums);
    printTreeNode(node);
    std::cout << "=================\n";
    std::vector<int> ret = findMode(node);
    printVector(ret);
}